package org.lys.controller;

import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.lys.mapper.IAdminMapper;
import org.lys.model.Admin;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 演示控制器
 *
 * @author liyansheng
 * @version 1.0.0
 * @date 2022/11/20
 */
@RequestMapping("/admin")
@RestController
public class AdminController {

    @Autowired
    private IAdminMapper adminMapper;


    @GetMapping("/getAll")
    @ApiOperation(value = "获取所有信息", notes = "获取信息")
    public String getAll() {
        List<Admin> admins = adminMapper.selectList(null);
        return admins.toString();
    }

    @DeleteMapping(value = "/deleteById/{id}")
    @ApiOperation(value = "根据id删除数据", notes = "删除用户")
    public String delete(@PathVariable Integer id) {
        int i = adminMapper.deleteById(id);
        if (i == 1) {
            return "删除成功！";
        } else {
            return "删除失败";
        }
    }

    @GetMapping("/getById/{id}")
    @ApiOperation(value = "根据id获取用户信息", notes = "根据id查询用户信息")
    public Admin getAdminById(@PathVariable Integer id) {
        return adminMapper.selectById(id);
    }


    @PostMapping(value = "/save")
    @ApiOperation(value = "添加管理员",notes = "添加管理员")
    @ApiImplicitParam(name = "admin",value = "管理员数据")
    public String saveAdmin(@RequestBody Admin admin) {
        int i = adminMapper.insert(admin);
        if (i == 1) {
            return "保存成功！";
        } else {
            return "保存失败";
        }
    }

    @PutMapping(value = "/updateById")
    @ApiOperation(value = "更新信息", notes ="更新信息")
    @ApiImplicitParam(name = "admin",value = "管理员数据")
    public String updateById(@RequestBody Admin admin) {
        int i = adminMapper.updateById(admin);
        if (i == 1) {
            return "修改成功！";
        } else {
            return "修改失败";
        }

    }
}
